/*
 * @Author: szx
 * @Date: 2022-02-16 14:38:30
 * @LastEditTime: 2022-02-16 15:04:34
 * @Description:
 * @FilePath: \leetcode\200-299\209\209_2.js
 */
/**
 * @param {number} target
 * @param {number[]} nums
 * @return {number}
 */
var minSubArrayLen = function (target, nums) {
    let res = Number.MAX_SAFE_INTEGER;
    let sum = 0;
    let left = 0;
    for (let i = 0; i < nums.length; i++) {
        sum += nums[i];
        while (sum >= target) {
            res = Math.min(res, i - left + 1);
            sum -= nums[left++];
            if (res === 1) return 1;
        }
    }

    return res == Number.MAX_SAFE_INTEGER ? 0 : res;
};
// console.log(minSubArrayLen(7, [2, 3, 1, 2, 4, 3]));
console.log(minSubArrayLen(4, [1, 4, 4]));
console.log(minSubArrayLen(11, [1, 1, 1, 1, 1, 1, 1, 1]));
